package cn.yoyo.dao;

import cn.yoyo.entity.Favourite;
import cn.yoyo.entity.Like;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class FavouriteDaoImpl implements FavouriteDao {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Favourite add(Favourite favourite) {
        return mongoTemplate.insert(favourite);
    }

    @Override
    public Favourite findById(String blogId,String userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("blogId").is(blogId));
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query,Favourite.class);
    }

    @Override
    public List<Favourite> findMyFavourites(String userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,Favourite.class);
    }

    @Override
    public void delete(String favouriteId) {
        Query query = new Query(Criteria.where("id").is(favouriteId));
        mongoTemplate.findAndRemove(query,Favourite.class);
    }
}
